﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GSPatEditor.Bitmaps
{
    class DDSFile : GSPatEditor.Pat.Image
    {
        private Bitmap bitmap;

        public static Pat.Image CreateDDSFromFile(string filename)
        {
            if (!File.Exists(filename))
            {
                return Pat.Image.Empty;
            }
            var ret = new DDSFile(filename);
            return ret;
        }

        private DDSFile(string filename)
        {
            //var dds = new DDSImage(File.ReadAllBytes(filename));
            //this.bitmap = dds.images[0];
            //throw new Exception();
            GSPatEditor.Bitmaps.DDSImpl.DDSGeneral.DDS_HEADER header;
            var stream = File.OpenRead(filename);
            var format = GSPatEditor.Bitmaps.DDSImpl.DDSGeneral.ParseDDSFormat(stream, out header);
            this.bitmap = GSPatEditor.Bitmaps.DDSImpl.DDSGeneral.LoadDDS(stream, header, format, 0)[0].Bitmap;
            base.Width = this.bitmap.Width;
            base.Height = this.bitmap.Height;
        }

        public override Bitmap CreateBitmapWithPalette(Pat.Palette pal)
        {
            return bitmap.Clone(new Rectangle(0, 0, Width, Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        }

        private static void Main()
        {
            var dir = @"E:\Games\[game]GRIEFSYNDROME\griefsyndrome\gs00_reduced\";
            int dxt5 = 0, dxt1 = 0;
            foreach (var file in Directory.EnumerateFiles(dir, "*.dds", SearchOption.AllDirectories))
            {
                GSPatEditor.Bitmaps.DDSImpl.DDSGeneral.DDS_HEADER header;
                using (var stream = File.OpenRead(file))
                {
                    var format = GSPatEditor.Bitmaps.DDSImpl.DDSGeneral.ParseDDSFormat(stream, out header);
                    if (format.InternalFormat == DDSImpl.ImageEngineFormat.DDS_DXT5)
                    {
                        dxt5++;
                    }
                    else if (format.InternalFormat == DDSImpl.ImageEngineFormat.DDS_DXT1)
                    {
                        dxt1++;
                    }
                    else
                    {

                    }
                }
            }
            //var dds = new DDSFile(filename);
        }
    }
}
